package cn.rongdao.jrkabao.ui.IdentificationVerificate.IdentificationWithValidationItem;

import android.graphics.Color;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.util.FRToast;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import butterknife.BindView;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.FRBaseLooperRequestActivity;
import cn.rongdao.jrkabao.bean.takephoto.ValidateIDCardInfoBean;
import cn.rongdao.jrkabao.model.EditUserIdentificationInfoModel;
import cn.rongdao.jrkabao.model.takephoto.IdCardNameAndNumberModel;
import cn.rongdao.jrkabao.ui.IdentificationVerificate.uicomponent.IDCardValidationUIComponent;
import cn.rongdao.jrkabao.ui.IdentificationVerificate.uicomponent.ValidationItemUIComponent;

/**
 * Created by jiangyongxing on 2017/9/19.
 * 描述：
 */

public abstract class IdentificationWithValidationItemActivity extends FRBaseLooperRequestActivity {

    @BindView(R.id.aivwvi_titlebar)
    protected TitleBarUIComponent titleBarUIComponent;
    @BindView(R.id.aivwvi_idcard_view)
    IDCardValidationUIComponent idCardValidationUIComponent;
    @BindView(R.id.aivwvi_verification_item)
    protected ValidationItemUIComponent mValidationItemUIComponent;

    @Override
    protected void initView() {
        super.initView();
        initData();
        idCardValidationUIComponent.setTitel("第一步  实名认证");
        initTitleBar();

        idCardValidationUIComponent.setContentDisable(View.VISIBLE);
        mValidationItemUIComponent.setContentDisable(View.VISIBLE);

        initListener();

    }

    private void initListener() {
        idCardValidationUIComponent.setOnIdCardVerificationSubmitListener(new IDCardValidationUIComponent.IdCardVerificationSubmitListener() {
            @Override
            public void OnIdCardVerificationSubmitListener(String name, String idCardNumber) {
                if (!validateNameAndNumber(name, idCardNumber))
                    return;
                if (idCardNumber.contains("x")) {
                    idCardNumber = idCardNumber.replace("x", "X");
                }
                saveUserIdCardData(name, idCardNumber);
            }
        });
    }


    private void saveUserIdCardData(final String name, final String idCardNumber) {
        EditUserIdentificationInfoModel.getInstance().execute(name, idCardNumber, new FRBaseObserver<String>(this, "正在保存，请稍候...") {
            @Override
            protected void onBaseNext(String data) {
                saveIdCardInfoSuccess(name, idCardNumber);
                FRToast.showToastSafe("身份信息保存成功");
            }
        });
    }

    private void saveIdCardInfoSuccess(String name, String idCardNumber) {
        idCardValidationUIComponent.setNameAndNumber(name, idCardNumber);
        mValidationItemUIComponent.setContentDisable(View.GONE);
    }

    private boolean validateNameAndNumber(String name, String idCardNumber) {
        if (TextUtils.isEmpty(name)) {
            FRToast.showToastSafe("姓名不能为空");
            return false;
        }

        if (TextUtils.isEmpty(idCardNumber)) {
            FRToast.showToastSafe("身份证号不能为空");
            return false;
        }
        if (idCardNumber.length() != 18) {
            FRToast.showToastSafe("身份证号格式不对");
            return false;
        }

        return true;
    }

    private void initData() {

        IdCardNameAndNumberModel.getInstance().execute(new FRBaseObserver<ValidateIDCardInfoBean>(this, "正在加载...") {
            @Override
            protected void onBaseNext(ValidateIDCardInfoBean data) {
                if (!TextUtils.isEmpty(data.getReal_name()) && !TextUtils.isEmpty(data.getIdentity_num())) {
                    idCardValidationUIComponent.setNameAndNumber(data.getReal_name(), data.getIdentity_num());
                    mValidationItemUIComponent.setContentDisable(View.GONE);
                } else {
                    idCardDataIsEmpty();
                    showInputMethod();
                }
            }

            @Override
            protected void onBaseError(Throwable t) {
                handingExceptions(t, false);
                idCardDataIsEmpty();
                showInputMethod();

            }
        });
    }

    private void showInputMethod() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                idCardValidationUIComponent.setEditTextFocus();
            }
        }, 500);
    }

    private void idCardDataIsEmpty() {
        mValidationItemUIComponent.setContentDisable(View.VISIBLE);
        idCardValidationUIComponent.setContentDisable(View.GONE);
    }

    private void initTitleBar() {
        titleBarUIComponent.initLeftBackBlack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        titleBarUIComponent.setBackgroundColor(Color.WHITE);
        titleBarUIComponent.initRightTvServiceBlack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showLoanConsultDialog();
            }
        });
    }


    @Override
    protected int layoutRes() {
        return R.layout.activity_identification_verificate_with_validation_item;
    }
}
